import os

import numpy as np
from osgeo import gdal


class EnviLdx:

    def __init__(self, file_path):
        self.file_path = file_path
        dataset = gdal.Open(file_path, gdal.GA_ReadOnly)
        if not dataset:
            raise FileNotFoundError(f"File {file_path} not found.")
        self.dataset = dataset
        bands = []
        for i in range(1, dataset.RasterCount + 1):
            band = dataset.GetRasterBand(i)
            bands.append(band.ReadAsArray())
        self.band_data = np.stack(bands, axis=-1)
        self.hdr_file_path = os.path.join(os.path.splitext(file_path)[0] + '.hdr')

    def __del__(self):
        self.dataset = None

    def close(self):
        self.dataset = None
